package com.soul.dj.ui.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.soul.dj.R;
import com.soul.dj.ui.view.ChildViewPager;
import com.soul.dj.util.Util;

/**
 * Created by soul on 2016/3/31.
 */

public class IndexFragment extends BaseFragment {

    private int menuCnt= 3;
    private int	s_width;
    private int	s_height;
    private ChildViewPager viewPager;
    private ImageView cursor;
    private LinearLayout listPagerMenu;

    public IndexFragment() {
    }

    @SuppressLint({ "JavascriptInterface", "SetJavaScriptEnabled" })
    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
        activity = getActivity();
        View v = inflater.inflate(R.layout.index_fragment,container,false);
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        s_width = dm.widthPixels;// 宽度
        s_height = dm.heightPixels;// 高度

        viewPager = (ChildViewPager)v.findViewById(R.id.listViewPager);
        viewPager.setOffscreenPageLimit(4);
        cursor = (ImageView) v.findViewById(R.id.cursor);
        listPagerMenu = (LinearLayout)v.findViewById(R.id.listPagerMenu);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(s_width / menuCnt , Util.dip2px(activity , 4));
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        cursor.setLayoutParams(params);

        FragmentManager fm = activity.getSupportFragmentManager();
        viewPager.setAdapter(new FragmentStatePagerAdapter(fm)
        {
            @Override
            public int getCount()
            {
                return 3;
            }

            @Override
            public Fragment getItem(int index)
            {
                switch(index)
                {
                    case 0:
                        return UnCheckAreaListFragment.newInstance("1");
                    case 1:
                        return UnCheckAreaListFragment.newInstance("2");
                    case 2:
                        return UnCheckAreaListFragment.newInstance("3");
                    default:
                        return UnCheckAreaListFragment.newInstance("1");
                }
            }
        });

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
        {
            @Override
            public void onPageSelected(int index)
            {

                for(int i = 0 ; i < listPagerMenu.getChildCount() ; i++)
                {

                    LinearLayout layout = (LinearLayout)listPagerMenu.getChildAt(i);
                    if (i== index){

                        selectLayout(layout,index);
                    }

                }
            }

            @Override
            public void onPageScrolled(int arg0 , float arg1 , int arg2)
            {
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)cursor.getLayoutParams();
                layoutParams.leftMargin = (int)((s_width/menuCnt)*(arg0+arg1));
                cursor.setLayoutParams(layoutParams);
            }

            @Override
            public void onPageScrollStateChanged(int arg0)
            {
            }
        });

        for(int i = 0 ; i < listPagerMenu.getChildCount() ; i++)
        {
            LinearLayout layout = (LinearLayout)listPagerMenu.getChildAt(i);
            if (i== 0){

                ImageView imageView = (ImageView) layout.getChildAt(0);
                TextView textView = (TextView) layout.getChildAt(1);
                textView.setTextColor(activity.getResources().getColor(R.color.black));
                imageView.setBackgroundResource(R.drawable.date_green);
            }
            final int index = i;
            layout.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    viewPager.setCurrentItem(index , false);
                }
            });
        }
        return  v;
    }

    private void selectLayout( LinearLayout layout,int index){

        ImageView imageView = (ImageView) layout.getChildAt(0);
        TextView textView = (TextView) layout.getChildAt(1);
        textView.setTextColor(activity.getResources().getColor(R.color.black));

        switch (index){
            case 0:
                imageView.setBackgroundResource(R.drawable.date_green);

                layout = (LinearLayout)listPagerMenu.getChildAt(1);
                imageView = (ImageView) layout.getChildAt(0);
                textView = (TextView) layout.getChildAt(1);
                textView.setTextColor(activity.getResources().getColor(R.color.main_menu_txt));
                imageView.setBackgroundResource(R.drawable.week);

                layout = (LinearLayout)listPagerMenu.getChildAt(2);
                imageView = (ImageView) layout.getChildAt(0);
                textView = (TextView) layout.getChildAt(1);
                textView.setTextColor(activity.getResources().getColor(R.color.main_menu_txt));
                imageView.setBackgroundResource(R.drawable.month);
                break;
            case 1:
                imageView.setBackgroundResource(R.drawable.week_green);

                layout = (LinearLayout)listPagerMenu.getChildAt(0);
                imageView = (ImageView) layout.getChildAt(0);
                textView = (TextView) layout.getChildAt(1);
                textView.setTextColor(activity.getResources().getColor(R.color.main_menu_txt));
                imageView.setBackgroundResource(R.drawable.date);

                layout = (LinearLayout)listPagerMenu.getChildAt(2);
                imageView = (ImageView) layout.getChildAt(0);
                textView = (TextView) layout.getChildAt(1);
                textView.setTextColor(activity.getResources().getColor(R.color.main_menu_txt));
                imageView.setBackgroundResource(R.drawable.month);

                break;
            case 2:
                imageView.setBackgroundResource(R.drawable.month_green);

                layout = (LinearLayout)listPagerMenu.getChildAt(0);
                imageView = (ImageView) layout.getChildAt(0);
                textView = (TextView) layout.getChildAt(1);
                textView.setTextColor(activity.getResources().getColor(R.color.main_menu_txt));
                imageView.setBackgroundResource(R.drawable.date);

                layout = (LinearLayout)listPagerMenu.getChildAt(1);
                imageView = (ImageView) layout.getChildAt(0);
                textView = (TextView) layout.getChildAt(1);
                textView.setTextColor(activity.getResources().getColor(R.color.main_menu_txt));
                imageView.setBackgroundResource(R.drawable.week);
                break;
        }
    }

}
